package com.example.lin.day9_14_md5;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = findViewById(R.id.editText);
        tv = findViewById(R.id.textView);

        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String s = et.getText().toString();
        switch (v.getId()) {
            case R.id.button:

                String s1 = MainActivity.md5(MainActivity.md5(s));
                tv.setText(s1);
                break;
            case R.id.button1:
                String s2 = base64UTF_8(s);
                tv.setText(s2);
                break;
            case R.id.button2:

                String s3 = base64UTF_8Decrypt(tv.getText().toString());
                tv.setText(s3);
                break;
        }
    }

    /**
     * 解密
     * @param base64Str
     * @return
     */
    public static String base64UTF_8Decrypt(String base64Str) {
        try {
            byte[] decode = Base64.decode(base64Str, Base64.DEFAULT);
            return new String(decode, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 加密
     * @param str
     * @return
     */
    public static String base64UTF_8(String str) {
        if (TextUtils.isEmpty(str))
            return "";
        try {
            String encodedString = Base64.encodeToString(str.getBytes("UTF-8"), Base64.DEFAULT);



            return encodedString;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";

    }


    /**
     * MD5 加密.原声MD5加密虽然不可逆.但已经有人运用大量加密后存储加密与未加密的结果.进行服务器验证.
     * 所以,一般可以在加密后 自己再添加一些字符串算法.实现属于自己的秘钥
     *
     * @param string
     * @return
     */
    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            int length = result.length();

            // 在加密后的结果 自己加入以下 规则.添油加醋
//            for (int i = 0; i < length; i++) {
//                result.insert(i * 2 + 1, "X");
//            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


}
